﻿using System;

using WebImageResizer.Utils;

namespace WebImageResizer.ImageStores
{
	public abstract class ImageStoreScavanger : IScheduleTask
	{
		#region Readonly & Static Fields

		private readonly TimeSpan m_ScavangeInterval;

		#endregion

		#region Fields

		private bool m_FirstRun = true;

		#endregion

		#region Constructors

		protected ImageStoreScavanger(TimeSpan scavangeInterval, bool startInitialScavange)
		{
			m_ScavangeInterval = scavangeInterval;
			m_FirstRun = startInitialScavange;
		}

		#endregion

		#region Instance Methods

		protected abstract void CleanOldImages();

		#endregion

		#region IScheduleTask Members

		public bool ReoccurringTask
		{
			get { return true; }
		}

		public DateTime GetNextReoccurrence()
		{
			if (m_FirstRun)
			{
				m_FirstRun = false;
				return DateTime.Now.AddSeconds(10);
			}

			return DateTime.Now.Add(m_ScavangeInterval);
		}

		public void RunTask()
		{
			CleanOldImages();
		}

		#endregion
	}
}